﻿package mortal.game.view.mainUI.smallMap.view
{
    import __AS3__.vec.*;
    import com.gengine.core.frame.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import flash.display.*;
    import flash.events.*;
    import frEngine.event.*;
    import mortal.game.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.events.*;
    import mortal.game.scene3D.map3D.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.view.common.*;
    import mortal.game.view.mainUI.smallMap.view.data.*;
    import mortal.mvc.core.*;

    public class SmallMapEmbedView extends GSprite
    {
        private var _bmpMap:GImageBitmap;
        private var _bmpMapSp:Sprite;
        private var _pathShower:SmallMapPathShower;
        private var _typeShower:SmallMapTypeShower;
        private var _mapPointSp:Sprite;
        private var _myPointSp:Sprite;
        private var _smallMapName:String;
        private var _showRoleDirection:Boolean;
        private var _mapScaleX:Number;
        private var _mapScaleY:Number;
        private var _point:Vector.<GBitmap>;
        private var _secTimer:SecTimer;
        public var loadMapCallBack:Function;
        private var _isSmallMapLoaded:Boolean;
        private var _mapLoaded:Boolean;

        public function SmallMapEmbedView(param1:Boolean)
        {
            this._point = new Vector.<GBitmap>;
            this._showRoleDirection = param1;
            this.isHideDispose = true;
            return;
        }// end function

        private function onSecTimerHandler(param1:SecTimer) : void
        {
            var _loc_4:GBitmap = null;
            var _loc_5:SmallMapTypeIconData = null;
            var _loc_7:Array = null;
            this.cleanAllPoint();
            var _loc_2:* = Cache.instance.smallmap.getAllChangabelDatas();
            var _loc_3:* = _loc_2.length;
            var _loc_6:int = 0;
            while (_loc_6 < _loc_3)
            {
                
                _loc_5 = _loc_2[_loc_6] as SmallMapTypeIconData;
                _loc_4 = UIFactory.gBitmap(_loc_5.iconName, 0, 0, this._mapPointSp);
                _loc_4.x = _loc_5.x * this._mapScaleX - _loc_4.width / 4;
                _loc_4.y = _loc_5.y * this._mapScaleY - _loc_4.height / 4;
                _loc_4.scaleX = 0.5;
                _loc_4.scaleY = 0.5;
                this._point.push(_loc_4);
                _loc_6++;
            }
            if (Cache.instance.group.isInGroup && Cache.instance.group.players.length > 1)
            {
                _loc_7 = Cache.instance.smallmap.getTeamates();
                _loc_6 = 0;
                while (_loc_6 < _loc_7.length)
                {
                    
                    _loc_5 = _loc_7[_loc_6] as SmallMapTypeIconData;
                    _loc_4 = UIFactory.gBitmap(ImagesConst.MapPoint_Teamate, 0, 0, this._mapPointSp);
                    _loc_4.x = _loc_5.x * this._mapScaleX - _loc_4.width / 2;
                    _loc_4.y = _loc_5.y * this._mapScaleY - _loc_4.height / 2;
                    this._point.push(_loc_4);
                    _loc_6++;
                }
            }
            this.roleWalkGridEndHandler();
            return;
        }// end function

        private function roleWalkGridEndHandler(param1 = null) : void
        {
            if (this._showRoleDirection)
            {
                this._myPointSp.x = RolePlayer.instance.x2d * this._mapScaleX;
                this._myPointSp.y = RolePlayer.instance.y2d * this._mapScaleY;
                this._myPointSp.rotation = RolePlayer.instance.direction + 90;
            }
            this._pathShower.showPath(Cache.instance.smallmap.paths, this._mapScaleX, this._mapScaleY);
            return;
        }// end function

        private function clearPaths(event:DataEvent) : void
        {
            Cache.instance.smallmap.clearPaths();
            this._pathShower.clearPaths();
            return;
        }// end function

        private function cleanAllPoint() : void
        {
            var _loc_1:GBitmap = null;
            for each (_loc_1 in this._point)
            {
                
                _loc_1.dispose();
            }
            this._point.splice(0, this._point.length);
            return;
        }// end function

        private function onSmallMapLoaded() : void
        {
            this._isSmallMapLoaded = true;
            this.setMapScale();
            if (this.loadMapCallBack != null)
            {
                this.loadMapCallBack.apply();
            }
            return;
        }// end function

        private function onMapLoaded(event:Event) : void
        {
            this._mapLoaded = true;
            this.setMapScale();
            return;
        }// end function

        private function setMapScale() : void
        {
            var _loc_1:Number = NaN;
            var _loc_2:Number = NaN;
            if (this._isSmallMapLoaded)
            {
                _loc_1 = this._bmpMap.width;
                _loc_2 = Game.mapInfo.gridWidth;
                this._mapScaleX = this._bmpMap.width / Game.mapInfo.gridWidth;
                this._mapScaleY = this._bmpMap.height / Game.mapInfo.gridHeight;
            }
            return;
        }// end function

        private function onClickHandler(event:MouseEvent) : void
        {
            var _loc_2:* = this._bmpMap.mouseX / this._mapScaleX;
            var _loc_3:* = this._bmpMap.mouseY / this._mapScaleY;
            Dispatcher.dispatchEvent(new DataEvent(EventName.SmallMapClick, {x:_loc_2, y:_loc_3, mapInfo:Game.mapInfo}));
            return;
        }// end function

        public function set smallMapName(param1:String) : void
        {
            this._smallMapName = param1;
            this._bmpMap.imgUrl = this._smallMapName;
            return;
        }// end function

        public function set showRoleDirection(param1:Boolean) : void
        {
            this._showRoleDirection = param1;
            this._myPointSp.visible = param1;
            return;
        }// end function

        public function run() : void
        {
            this._secTimer.start();
            this._secTimer.addListener(TimerType.ENTERFRAME, this.onSecTimerHandler);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._bmpMapSp = UIFactory.sprite(0, 0, this);
            this._bmpMap = UIFactory.gImageBitmap("", 0, 0, this._bmpMapSp, this.onSmallMapLoaded);
            this._pathShower = UICompomentPool.getUICompoment(SmallMapPathShower) as SmallMapPathShower;
            this._pathShower.showHideSelfIcon(false);
            UIFactory.setObjAttri(this._pathShower, 0, 0, -1, -1, this);
            this._mapPointSp = UIFactory.sprite(0, 0, this);
            this._mapPointSp.mouseEnabled = false;
            this._myPointSp = UIFactory.sprite(0, 0, this);
            this._myPointSp.mouseEnabled = false;
            pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.MapPoint_Self2, -8, -11, this._myPointSp));
            this._secTimer = new SecTimer(1);
            MapLoader.instance.addEventListener(Event.COMPLETE, this.onMapLoaded);
            FrEventDispatcher.instance.proxyAddEventListener(RolePlayer.instance, PlayerEvent.GIRD_WALK_END, this.roleWalkGridEndHandler);
            this._bmpMapSp.addEventListener(MouseEvent.CLICK, this.onClickHandler);
            this._bmpMapSp.mouseEnabled = true;
            Dispatcher.addEventListener(EventName.AI_Canceled, this.clearPaths);
            Dispatcher.addEventListener(EventName.AI_AllFinished, this.clearPaths);
            return;
        }// end function

        public function showBoosPoint(param1:int, param2:Number, param3:Array) : void
        {
            if (this._typeShower == null)
            {
                this._typeShower = UICompomentPool.getUICompoment(SmallMapTypeShower) as SmallMapTypeShower;
                this._typeShower.isShowName(false);
                this._typeShower.updateTypeShow(param1, param2, param3);
                this.addChild(this._typeShower);
            }
            return;
        }// end function

        public function get bmpMap() : GImageBitmap
        {
            return this._bmpMap;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._bmpMapSp.removeEventListener(MouseEvent.CLICK, this.onClickHandler);
            MapLoader.instance.removeEventListener(Event.COMPLETE, this.onMapLoaded);
            FrEventDispatcher.instance.proxyRemoveEventListener(RolePlayer.instance, PlayerEvent.GIRD_WALK_END, this.roleWalkGridEndHandler);
            Dispatcher.removeEventListener(EventName.AI_Canceled, this.clearPaths);
            Dispatcher.removeEventListener(EventName.AI_AllFinished, this.clearPaths);
            this._bmpMapSp = null;
            this.cleanAllPoint();
            this._smallMapName = "";
            this._mapLoaded = false;
            this._isSmallMapLoaded = false;
            this._showRoleDirection = false;
            this._bmpMap.dispose(param1);
            this._bmpMap = null;
            this._secTimer.dispose(param1);
            this._secTimer = null;
            this._pathShower.dispose(param1);
            this._pathShower = null;
            UIFactory.disposeSprite(this._mapPointSp);
            this._mapPointSp = null;
            UIFactory.disposeSprite(this._myPointSp);
            this._myPointSp = null;
            if (this._typeShower)
            {
                this._typeShower.dispose(param1);
                this._typeShower = null;
            }
            this.loadMapCallBack = null;
            super.disposeImpl(param1);
            return;
        }// end function

    }
}
